package com.yushu.authority.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.yushu.authority.mapper.PmRoleMapper;
import com.yushu.authority.pojo.bo.PmMenu;
import com.yushu.authority.pojo.bo.PmOrg;
import com.yushu.authority.pojo.bo.PmRole;
import com.yushu.authority.pojo.bo.PmUser;
import com.yushu.authority.pojo.vo.PmRoleVO;
import com.yushu.authority.pojo.vo.PmUserVO;
import com.yushu.authority.service.PmRoleService;
import com.yushu.common.execption.CgException;
import com.yushu.common.service.impl.YSBaseServiceImpl;
import com.yushu.common.shiro.ShiroUtil;
import com.yushu.common.util.collection.CollectionUtil;
import com.yushu.common.util.date.DateUtils;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.string.CamelCaseUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.*;

 

/**
 * 
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
@Service("pmRoleService")
public class PmRoleServiceImpl extends YSBaseServiceImpl<PmRoleMapper, PmRole> implements PmRoleService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PmRoleService roleService;

    public Pages<PmRoleVO> page(Pages<PmRoleVO> page) {
        PmUser sessionUser = ShiroUtil.getSessionUser();
        EntityWrapper wrapper = new EntityWrapper<PmRoleVO>();
        PmRoleVO pmRole = page.getData();
        if (null != pmRole) {
            if (null != pmRole.getCreateTimeBefore() && null != pmRole.getCreateTimeAfter()) {
                String dateBefore = DateUtils.dateToString(pmRole.getCreateTimeBefore());
                String dateAfter = DateUtils.dateToString(pmRole.getCreateTimeAfter());
                wrapper.between("t.create_time", dateBefore, dateAfter);
            }
            if (StringUtils.isNotEmpty(pmRole.getUpdateUserId())) {
                wrapper.like("t.update_user_id", pmRole.getUpdateUserId(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getOrgId())) {
                wrapper.like("t.org_id", pmRole.getOrgId(), SqlLike.DEFAULT);
            }
            if (null != pmRole.getUpdateTimeBefore() && null != pmRole.getUpdateTimeAfter()) {
                String dateBefore = DateUtils.dateToString(pmRole.getUpdateTimeBefore());
                String dateAfter = DateUtils.dateToString(pmRole.getUpdateTimeAfter());
                wrapper.between("t.update_time", dateBefore, dateAfter);
            }
            if (StringUtils.isNotEmpty(pmRole.getDescription())) {
                wrapper.like("t.description", pmRole.getDescription(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getUserId())) {
                wrapper.like("t.user_id", pmRole.getUserId(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getRoleSign())) {
                wrapper.like("t.role_sign", pmRole.getRoleSign(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getRoleType())) {
                wrapper.like("t.role_type", pmRole.getRoleType(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getRoleName())) {
                wrapper.like("t.role_name", pmRole.getRoleName(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(pmRole.getId())) {
                wrapper.like("t.id", pmRole.getId(), SqlLike.DEFAULT);
            }
            if (StringUtils.isNotEmpty(page.getSort())) {
                wrapper.orderBy(CamelCaseUtil.toUnderlineName(page.getSort()), "asc".equals(page.getOrder()) ? true : false);
            }
        }
        List<PmRoleVO> list = baseMapper.selectPageList(page, wrapper);
        page.setRecords(list);
        return page;
    }


    /**
     * 批量删除角色
     *
     * @param pmRoles
     * @return
     */
    @Override
    public boolean deleteBatch(List<PmRole> pmRoles) throws CgException {
        boolean deleteState = false;
        try {
            if (CollectionUtils.isNotEmpty(pmRoles)) {
                int countRolesOnuser = roleService.queryUserCountOnRole(pmRoles);
                if (countRolesOnuser > 0) {
                    throw new CgException("当前角色下绑定用户，不允许删除");
                }
                List<String> ids = new ArrayList<String>();
                for (int i = 0; i < pmRoles.size(); i++) {
                    PmRole pmRole = pmRoles.get(i);
                    ids.add(pmRole.getId());
                }
                deleteState = deleteBatchIds(ids);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CgException(e.getMessage());
        }
        return deleteState;
    }


    /**
     * 通过主键更新角色的缓存数据
     *
     * @param pmRole
     * @return
     */
    //@CachePut(value = "pmRole", key = "#pmRole.id")
    public PmRole updateByKey(PmRole pmRole) throws CgException {
        try {
            PmUser sessionUser = ShiroUtil.getSessionUser();
            PmUserVO userCache = (PmUserVO) redisTemplate.opsForValue().get("userSession_" + sessionUser.getId());
            pmRole.setUpdateUserId(sessionUser.getId());
            pmRole.setUpdateTime(new Date());
            super.updateById(pmRole);
        } catch (RuntimeException e) {
            //e.printStackTrace();
            throw new CgException(e.getMessage());
        }
        return pmRole;
    }

    /**
     * 保存的时候清除所有角色的缓存数据
     *
     * @param pmRole
     * @return
     */
    //@CacheEvict(value = "pmRole",allEntries=true)
    @Override
    public boolean insert(PmRole pmRole) throws CgException {
        try {
            PmUser sessionUser = ShiroUtil.getSessionUser();
            pmRole.setUserId(sessionUser.getId());
            pmRole.setCreateTime(new Date());
            pmRole.setId(IdWorker.get32UUID());
            return super.insert(pmRole);
        } catch (RuntimeException e) {
            throw new CgException(e.getMessage());
        }
    }


    /**
     * 删除的时候同步角色的缓存数据
     *
     * @param id 主键
     * @return
     */
    //@CacheEvict(value = "pmRole", key = "#id")
    @Override
    public boolean deleteById(Serializable id) {
        /**
         * 通过角色id查询用户是否绑定用户
         * 如果角色被用户绑定则提示异常，删除失败
         * 如果角色无用户绑定则提示是否删除
         */
        String ids = (String) id;

        int countUsers = this.selectUserByRoleId(ids);

        if (countUsers > 0) {
            throw new CgException("此角色绑定用户，禁止删除！");
        }
        int countMenus = this.selectMenusByRoleId(ids);

        if (countMenus > 0) {
            throw new CgException("此角色绑定菜单，禁止删除！");
        }
        return super.deleteById(id);

    }
    
    
     @Override
    public int selectMenusByRoleId(String ids) {
        return baseMapper.selectMenusByRoleId(ids);
    }


    /**
     * 查询时保存角色的缓存数据
     *
     * @param id 主键
     * @return
     */
    //@Cacheable(value = "pmRole", key = "#id")
    @Override
    public PmRole selectById(Serializable id) {
        return super.selectById(id);
    }

     @Override
    public String checkRepeat(Map map) {
        String name = map.get("name") + "";
        String value = map.get("value") + "";
        String key = map.get("key") + "";
        int count = baseMapper.queryCountByName(map);
//        如果key存在，则是修改，否则是新增
        if (StringUtils.isNotBlank(key)) {
            /*如果是更新，则数字可以大于1*/
            if (count <= 0) {
                return "true";
            } else if (count > 1) {//说明存在多个
                return "false";
            } else if (count == 1) {

                EntityWrapper wrapper = new EntityWrapper<PmRole>();
                wrapper.eq(name, value);
                List<PmRole> pmRoles = baseMapper.selectList(wrapper);
                String checkExit = "true";
                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(pmRoles)) {
                    for (int i = 0; i < pmRoles.size(); i++) {
                        PmRole pmRole = pmRoles.get(i);
//                        说明当前值象存在于不同的对象之中，也不能重复
                        if (!StringUtils.equals(pmRole.getId().trim(), key.trim())) {
                            checkExit = "false";
                        }
                    }
                }
                return checkExit;
            } else {
                return "true";
            }
        } else {
            /*如果是新增，则数字可以大于0*/
            if (count > 0) {
                return "false";
            } else {
                return "true";
            }
        }
    }

    @Override
    public List<PmRole> selectList(PmRole pmRole) {
        EntityWrapper wrapper = new EntityWrapper<PmRole>();

        if (StringUtils.isNotEmpty(pmRole.getUpdateUserId())) {
            wrapper.eq("update_user_id", pmRole.getUpdateUserId());
        }
        if (StringUtils.isNotEmpty(pmRole.getOrgId())) {
            wrapper.eq("org_id", pmRole.getOrgId());
        }
        if (StringUtils.isNotEmpty(pmRole.getDescription())) {
            wrapper.eq("description", pmRole.getDescription());
        }
        if (StringUtils.isNotEmpty(pmRole.getUserId())) {
            wrapper.eq("user_id", pmRole.getUserId());
        }
        if (StringUtils.isNotEmpty(pmRole.getRoleSign())) {
            wrapper.eq("role_sign", pmRole.getRoleSign());
        }
        if (StringUtils.isNotEmpty(pmRole.getRoleType())) {
            wrapper.eq("role_type", pmRole.getRoleType());
        }
        if (StringUtils.isNotEmpty(pmRole.getRoleName())) {
            wrapper.eq("role_name", pmRole.getRoleName());
        }
        if (StringUtils.isNotEmpty(pmRole.getId())) {
            wrapper.eq("id", pmRole.getId());
        }
        List<PmRole> list = selectList(wrapper);
        return list;
    }


    /**
     * <pre>通过用户查询角色*
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/24 19:56
     */
    @Override
    public List<PmRole> selectRolesByUserId(String dataBaseId, String userId) {
        return baseMapper.selectRolesByUserId(userId);
    }

    //更新用户角色分配
    @Override
    public void updateRoleOnUser(Map roleUserData) {
        PmUser userVO = ShiroUtil.getSessionUser();
        try {
            //定义集合存储用户和角色ID
            List<String> userIds = (List<String>) roleUserData.get("userIds");
            List<String> roleIds = (List<String>) roleUserData.get("roleIds");
            List<Map<String, Object>> roleUserQueryList = new ArrayList<>();
            /**
             * 如果用户和角色的id同时大于0
             */
            if (userIds.size() > 0 && roleIds.size() > 0) {
                //嵌套循环遍历出用户和角色id
                for (int i = 0; i < userIds.size(); i++) {
                    for (int j = 0; j < roleIds.size(); j++) {
                        //遍历出的id存入String中
                        String userId = userIds.get(i);
                        String roleId = roleIds.get(j);
                        //将ID存入Map queryData中
                        Map<String, Object> queryData = new HashMap<>();
                        queryData.put("userId", userId);
                        queryData.put("roleId", roleId);
                        //id加入到集合中去
                        roleUserQueryList.add(queryData);
                    }
                }
            }
            //取消用户角色
            //定义取消用户角色条件NotRoleUserQueryList
            List<Map<String, Object>> NotRoleUserQueryList = new ArrayList<>();
            //如果用户只有ID，说明未赋予角色，或者取消角色
            if (userIds.size() > 0 && roleIds.size() == 0) {
                for (int i = 0; i < userIds.size(); i++) {
                    //遍历出的id存入String中
                    String userId = userIds.get(i);
                    //将ID存入Map queryData中
                    Map<String, Object> queryData = new HashMap<>();
                    queryData.put("userId", userId);
                    //id加入到集合中去
                    NotRoleUserQueryList.add(queryData);
                }
            }
            /*if (notRoleIds.size() > 0) {
                for (int i = 0; i < notRoleIds.size(); i++) {

                    String roleId = notRoleIds.get(i);
                    Map queryData = new HashMap();
                    queryData.put("roleId", roleId);
                    NotRoleUserQueryList.add(queryData);
                }
            }*/
            //如果NotRoleUserQueryList不为空的话
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(NotRoleUserQueryList)) {
                //批量删除用户角色
                baseMapper.batchDeleteRoleOnUsers(NotRoleUserQueryList);
            }
            //如果roleUserQueryList（存的userids和roleids）集合不为空时
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(roleUserQueryList)) {
                //批量删除用户角色
                baseMapper.batchDeleteRoleOnUsers(roleUserQueryList);
                //插入用户角色
                baseMapper.insertRoleUser(roleUserQueryList);
            }
            redisTemplate.delete("SysMenuService_queryMenuTree_" + userVO.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

     //更新角色菜单
    @Override
    public void updateMenuOnRole(Map roleMenuData) {
        PmUserVO userVO = ShiroUtil.getSessionUser();
        try {
            /*获取菜单是否有数据*/
            List<Map> menus = (List<Map>) roleMenuData.get("menus");
            /*获取角色所选中的菜单数据*/
            List<String> roles = (List<String>) roleMenuData.get("roles");
            /*获取角色勾选的菜单*/
            List<Map<String, String>> menuOnRoleMaps = new ArrayList<Map<String, String>>();

            /*如果菜单不为空*/
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(menus)) {
                for (int i = 0; i < menus.size(); i++) {
                    Map map = menus.get(i);
                    //或取菜单id
                    String menuId = map.get("id") + "";
                    //如果角色不为空
                    if (org.apache.commons.collections.CollectionUtils.isNotEmpty(roles)) {
                        for (int j = 0; j < roles.size(); j++) {
                            //获取角色id
                            String roleId = roles.get(j);
                            /*角色id和菜单id绑定 存入Map*/
                            Map<String, String> roleMenuMap2 = new HashMap<String, String>();
                            roleMenuMap2.put("roleId", roleId);
                            roleMenuMap2.put("menuId", menuId);
                            /*获取角色勾选的菜单*/
                            menuOnRoleMaps.add(roleMenuMap2);
                        }

                    }

                }
            }
            /*
            如果菜单为空 角色不为空
            执行删除菜单数据
            * */
            if (CollectionUtils.isNotEmpty(roles)) {
                for (int j = 0; j < roles.size(); j++) {
                    //获取角色id
                    String roleId = roles.get(j);
                    /*角色id 存入Map*/
                    Map<String, String> roleMenuMap = new HashMap<String, String>();
                    roleMenuMap.put("roleId", roleId);
                    /*获取角色勾选的菜单*/
                    menuOnRoleMaps.add(roleMenuMap);
                }
                //先批量删除菜单角色关联表
                batchDeleteMenuOnRole(menuOnRoleMaps);
                if (CollectionUtils.isNotEmpty(menus)) {
                    //再批量插入菜单角色关联表
                    batchInsertMenuOnRole(menuOnRoleMaps);
                }
            }
            //更新缓存
            redisTemplate.delete("SysMenuService_queryMenuTree_" + userVO.getId());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * <pre>查询已选择角色的菜单的交集
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/26 11:18
     */
    @Override
    public List<PmMenu> queryIntersections(String[] roleIds) {
        List<PmMenu> list = new ArrayList<PmMenu>();
        List<String> roleIdList = Arrays.asList(roleIds);
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(Arrays.asList(roleIds))) {
            for (int i = 0; i < roleIdList.size(); i++) {
                if (roleIdList.size() > 1) {
                    String roleID1 = roleIdList.get(i);

                    if (i < roleIdList.size() - 1) {
                        String roleID2 = roleIdList.get(i + 1);
                        list = baseMapper.selectMenuByRole(roleID1);
                        List<PmMenu> list2 = baseMapper.selectMenuByRole(roleID2);
                        list = CollectionUtil.intersection(list, list2);

                    }
                } else {
                    String roleID1 = roleIdList.get(i);
                    list = baseMapper.selectMenuByRole(roleID1);
                }
            }
        }
        return list;
    }
    //通过用户查询角色
    public List<PmRole> selectRoleByUser(PmUserVO user) {
        try {
            return baseMapper.selectRoleByUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<PmRole>();
        }
    }
    
    
    /**
     * <pre>询当前所选择用户下的角色的交集
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/31 17:13
     */
//    @Cacheable(value = "PmRole", key = "#users[0].id")
    @Override
    public List<PmRole> queryRolesOnUsersIntersections(List<PmUserVO> users) {
        List<PmRole> finalRoleList = new ArrayList<PmRole>();
        try {
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(users)) {
                for (int i = 0; i < users.size(); i++) {
                    PmUserVO userVO = users.get(i);
                    if (users.size() > 1) {
                        if (i < users.size() - 1) {
                            PmUserVO user = users.get(i + 1);
                            finalRoleList = selectRoleByUser(userVO);
                            List<PmRole> list2 = selectRoleByUser(user);
                            finalRoleList = CollectionUtil.intersection(finalRoleList, list2);

                        }
                    } else {

                        finalRoleList = selectRoleByUser(userVO);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalRoleList;
    }


    /**
     * <pre> 更新角色搜索框权限关联表
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/8/7 10:09
     */
    @Override
    public void updateSearchBoxOnRole(Map boxRoleData) {

        try {
            List<Map> searchBox = (List<Map>) boxRoleData.get("searchBox");
            List<String> roles = (List<String>) boxRoleData.get("roles");
            List<Map<String, String>> boxOnRoleMaps = new ArrayList<Map<String, String>>();
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(searchBox)) {
                for (int i = 0; i < searchBox.size(); i++) {
                    Map searchBoxMap = searchBox.get(i);
                    String boxId = searchBoxMap.get("id") + "";
                    if (org.apache.commons.collections.CollectionUtils.isNotEmpty(roles)) {
                        for (int j = 0; j < roles.size(); j++) {
                            String roleId = roles.get(j);
                            Map<String, String> roleMenuMap2 = new HashMap<String, String>();
                            roleMenuMap2.put("roleId", roleId);
                            roleMenuMap2.put("boxId", boxId);
                            boxOnRoleMaps.add(roleMenuMap2);
                        }

                    }

                }
            }
//        先批量删除搜索框角色关联表
            batchDeleteSearchOnRole(boxOnRoleMaps);
//        再批量插入搜索框角色关联表
            batchInsertSearchOnRole(boxOnRoleMaps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * <p>
     * 保存用户和角色
     * </p>
     *
     * @param user 当前用户
     * @return void
     * @author test
     * @since 2019/11/8  12:11
     */
    @Override
    public void saverUserAndRole(PmUser user) {
        List<Map<String, Object>> roleUserQueryList = new ArrayList<>();
        //        2019-12-31 15:04:44 FIXME 功能待完成
        EntityWrapper wrapper = new EntityWrapper<PmRole>();
        wrapper.eq("role_type", "00020003");//客户人员级别角色
        List<PmRole> pmRoles = baseMapper.selectList(wrapper);
//        queryData.put("roleId", "7b560c1ce8af4419bbe7f3e82f3eac12");
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(pmRoles)) {
            for (PmRole pmRole : pmRoles) {
                Map<String, Object> queryData = new HashMap<>();
                queryData.put("userId", user.getId());
                queryData.put("roleId", pmRole.getId());
                roleUserQueryList.add(queryData);
            }
        }
        baseMapper.insertRoleUser(roleUserQueryList);
    }

    /**
     * <pre>
     * 批量删除用户角色关联表
     * </pre>
     *
     * @param userMaps 用户外键
     * @return void
     * @author test
     * @since 2019/11/8  12:34
     **/
     @Override
    public void batchDeleteRoleOnUsers(List<Map<String, Object>> userMaps) {
        baseMapper.batchDeleteRoleOnUsers(userMaps);
    }

    /*通过角色ID查询  是否绑定用户*/
   @Override
    public int selectUserByRoleId(String id) {
        return baseMapper.selectUserByRoleId(id);
    }

    /**
     * <pre>删除表的同时，需要删除和表相关联的角色，权限
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/8/27 13:32
     */
  /*  @Override
    public void deleteBatchOnTables(List<ProjectTablesVO> tablesList) {


    }*/

    /**
     * <pre>查询当前用户下的角色
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/31 15:31
     */
    @Override
    public List<PmRole> queryRolesOnUsers(List<PmUserVO> users) {
        return baseMapper.queryRolesOnUsers(users);
    }


    /**
     * <pre>批量插入菜单角色关联表
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/25 21:42
     */
    public void batchInsertMenuOnRole(List<Map<String, String>> menuOnRoleMaps) throws RuntimeException {
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(menuOnRoleMaps)) {
            baseMapper.batchInsertMenuOnRole(menuOnRoleMaps);
        }
    }

    /**
     * <pre>批量删除菜单角色关联表
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019/7/25 21:42
     */
    public void batchDeleteMenuOnRole(List<Map<String, String>> menuOnRoleMaps) throws RuntimeException {
        if (CollectionUtils.isNotEmpty(menuOnRoleMaps)) {
            baseMapper.batchDeleteMenuOnRole(menuOnRoleMaps);
        }
    }

    /**
     * <pre>批量插入搜索框角色关联表
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019-8-7 10:23:05
     */
   public void batchInsertSearchOnRole(List<Map<String, String>> searchOnRoleMaps) throws RuntimeException {
        baseMapper.batchInsertSearchOnRole(searchOnRoleMaps);
    }

    /**
     * <pre>批量删除搜索框角色关联表
     * </pre>
     *
     * @param
     * @return
     * @author test
     * @version 1.0
     * @since 2019-8-7 10:23:10
     */
    public void batchDeleteSearchOnRole(List<Map<String, String>> searchOnRoleMaps) throws RuntimeException {
        baseMapper.batchDeleteSearchOnRole(searchOnRoleMaps);
    }


    @Override
    public PmRoleVO queryRoleByKey(String id) {
        return baseMapper.queryRoleByKey(id);
    }


    @Override
    public boolean updateById(PmRole role) {
        PmUser sessionUser = ShiroUtil.getSessionUser();
        if (null != sessionUser) {
            role.setUpdateUserId(sessionUser.getId());
        }
        role.setUpdateTime(new Date());
        return super.updateById(role);
    }


    /**
     * 查询当前项目下的角色
     *
     * @return
     */
    @Override
    public List<PmRole> queryProjectRoles() {

        PmUser sessionUser = ShiroUtil.getSessionUser();
        PmUserVO userCache = (PmUserVO) redisTemplate.opsForValue().get("userSession_" + sessionUser.getId());

        EntityWrapper wrapper = new EntityWrapper<PmRole>();
        List<PmRole> pmRoles = baseMapper.selectList(wrapper);
        return pmRoles;
    }
     //查询角色下是否绑定用户
    @Override
    public int queryUserCountOnRole(List<PmRole> pmRoles) {
        if (CollectionUtils.isNotEmpty(pmRoles)) {
            return baseMapper.queryUserCountOnRole(pmRoles);
        } else {
            return 0;
        }
    }
    //增加用户角色
     @Override
    public boolean saveRole(PmRole pmRole) {
        return baseMapper.saveRole(pmRole);
    }
    
    @Override
    public String validateRoleName(String roleName, String key) {
        EntityWrapper wrapper = new EntityWrapper<PmRole>();
        wrapper.eq("role_name",roleName);
        String primaryKey = "";
        String validateInfo = super.validateNameRepeatByKey(key, primaryKey, wrapper);
        return validateInfo;
    }
 }

